package com.nba.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.nba.bean.Pdate;
import com.nba.bean.inherit.PdateInherit;
import com.nba.dao.PdateDao;
import com.nba.util.DBUtil;

public class PdateDaoImpl implements PdateDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	
	@Override
	public List<Pdate> selectPdateAll() {
		String sql = "SELECT * FROM pdate";
		return jt.query(sql, new BeanPropertyRowMapper<Pdate>(Pdate.class));
	}

	@Override
	public Pdate selectPdateById(int pdateId) {
		String sql = "SELECT * FROM pdate WHERE pdate_id = ?";
		List<Pdate> list = jt.query(sql, new BeanPropertyRowMapper<Pdate>(Pdate.class),pdateId);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public int insertPdate(Pdate pdate) {
		String sql  = "INSERT INTO pdate (player_id,player_date,rteam_id,start_num,pdate_time,all_hit,all_move,three_hit,three_move,free_hit,free_move,attack,defense,score,backboard,assists,snatch,nutcap,fault,breakrules,season_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql, pdate.getPlayerId(),pdate.getPlayerDate(),pdate.getRteamId(),pdate.getStartNum(),pdate.getPdateTime(),pdate.getAllHit(),pdate.getAllMove(),pdate.getThreeHit(),pdate.getThreeMove(),pdate.getFreeHit(),pdate.getFreeMove(),pdate.getAttack(),pdate.getDefeanse(),pdate.getScore(),pdate.getBackboard(),pdate.getAssists(),pdate.getSnatch(),pdate.getNutcap(),pdate.getFault(),pdate.getBreakrules(),pdate.getSeasonId());
	}

	@Override
	public int deletePdate(int pdateId) {
		String sql = "DELETE FROM pdate WHERE pdate_id = ?";
		return jt.update(sql, pdateId);
	}

	@Override
	public int updatePdate(Pdate pdate) {
		String sql = "UPDATE pdate SET player_id = ?,player_date = ?,rteam_id = ?,start_num = ?,pdate_time =?,all_hit =?,all_move = ?,three_hit = ?,three_move =?,free_hit =?,free_move = ?,attack = ?,defeanse = ?,score = ?,backboard =?,assists =?,snatch = ?,nutcap =?,fault =?,breakrules = ?,season_id = ? WHERE pdate_id = ?";
		return jt.update(sql, pdate.getPlayerId(), pdate.getPlayerDate() ,pdate.getRteamId() ,pdate.getStartNum(), pdate.getPdateTime() ,pdate.getAllHit(),pdate.getAllMove(),pdate.getThreeHit(),pdate.getThreeMove(),pdate.getFreeHit(),pdate.getFreeMove(),pdate.getAttack(),pdate.getDefeanse(),pdate.getScore(),pdate.getBackboard(),pdate.getAssists(),pdate.getSnatch(),pdate.getNutcap(),pdate.getFault(),pdate.getBreakrules(),pdate.getSeasonId(),pdate.getPdateId());
	}

	@Override
	public List<PdateInherit> selectPdateAll(String keyword, int pageNum, int pageSize) {
		String sql = "SELECT * FROM ppts WHERE player_name LIKE ? OR rteam_name LIKE ? OR season_name LIKE ? LIMIT ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<PdateInherit>(PdateInherit.class) ,"%"+keyword+"%","%"+keyword+"%","%"+keyword+"%",(pageNum-1)*pageSize ,pageSize);
	}

	@Override
	public int selectPdateTotal(String keyword) {
		String sql = "SELECT count(1) FROM ppts WHERE player_name LIKE ? OR rteam_name LIKE ? OR season_name LIKE ?";
		return jt.queryForObject(sql, Integer.class,"%"+keyword+"%","%"+keyword+"%","%"+keyword+"%");
	}

}
